package com.sk.controller;

import com.sk.pojo.RespBean;
import com.sk.server.UserListServer;
import com.sk.service.impl.SUserServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/userlist")
public class UserListController {

    @Autowired
    UserListServer userListServer;

    @PostMapping("getRole")
    public String getRole(HttpServletRequest request){
        return request.getSession().getAttribute("role").toString();
    }

    @PostMapping("like")
    public RespBean like(String tel, String username, HttpServletRequest request) {
        return userListServer.like(tel, username, request);
    }

    @PostMapping("delete")
    public RespBean delete(@RequestParam("id") int id,int rid) {
        return userListServer.delete(id,rid);
    }


    @PostMapping("update")
    public RespBean update(Integer id,String username,
                           String tel,String pwd,MultipartFile file,
                           Integer sex, Integer age, String toRole,
                           HttpServletRequest request){
        Integer rid = (Integer) request.getSession().getAttribute("rid");

        //管理员用户不能修改role属性,只有超级管理员可以
        if(rid == 2){
            return userListServer.update(id,username,tel,pwd,file
                    //toRole参数普通管理员无权更改,为null
                    ,sex,age,"",rid);
        }

        if(rid == 1){
            return userListServer.update(id,username,tel,pwd,file
                    ,sex,age,toRole,rid);
        }

        return RespBean.error("修改失败");

    }


    @PostMapping("insert")
    public RespBean insert (String username,
                            String tel,String pwd,MultipartFile file,
                            Integer sex, Integer age, String toRole,
                            HttpServletRequest request){
        return userListServer.insert(username,tel,pwd,file,sex,age,toRole,request);

    }




//    @PostMapping("/update")
//    public void t666(int s){
//        System.out.println(s);
//    }
}
